package main

import (
	"encoding/json"
	"fmt"
)

//将json字符串反序列化成结构体
func main() {

	p := Person{
		Name: "zhangsan",
		Age:  11,
		Addr: "浙江舟山",
	}

	str, error := json.Marshal(p)
	if error != nil {
		fmt.Println("convert failed")
	}

	fmt.Println("json字符串 ", string(str))

	var p1 Person
	err := json.Unmarshal(str, &p1)

	if err != nil {
		fmt.Println("reSerial failed", err)
	}

	fmt.Println(p1)

	fmt.Println("--------------------------")

	str2 := "{\"name\":\"zhangsan\",\"age\":11,\"addr\":\"浙江舟山\"}"

	var p3 Person
	json.Unmarshal([]byte(str2), &p3)
	fmt.Println("字符串反序列化结果 ", p3)

}

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
	Addr string `json:"addr"`
}
